<?php

namespace app\model;

use think\Model;

class Users extends Model
{
    protected $table = 'yx_sys_users';
    protected $pk = 'id';

    public function getJoindateAttr($value)
    {
        if ($value > 0) {
            return date('Y-m-d', $value);
        }
        return '';
    }

    public function getStarttimeAttr($value)
    {
        if ($value > 0) {
            return date('Y-m-d', $value);
        }
        return '';
    }

    public function getEndtimeAttr($value)
    {
        if ($value > 0) {
            return date('Y-m-d', $value);
        } elseif ($value == 0) {
            return '永久';
        }
    }

    function userHash($password, $salt)
    {
        $authkey = config('database.serverkey') ? config('database.serverkey') : '4432a9d7';
        $password = "{$password}-{$salt}-{$authkey}";
        return sha1($password);
    }

    //易宣修改-新增自动添加用户和密码
    function getPassword($type, $length = 8)
    {
        // 密码字符集，可任意添加你需要的字符
        if ($type == 1) {
            $chars = '12356789';
        } else {
            $chars = 'abcdefghijkmnopqrstuvwxyz';
        }
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }


    function random($length, $numeric = FALSE)
    {
        $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
        $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
        if ($numeric) {
            $hash = '';
        } else {
            $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
            $length--;
        }
        $max = strlen($seed) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $seed{mt_rand(0, $max)};
        }
        return $hash;
    }


}